package com.fh.controller.houtai.examinationcontroller;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.constant.BusinessConstant;
import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.Menu;
import com.fh.service.houtai.examinationcontroller.ExaminationControllerService;
import com.fh.service.houtai.examinationcontroller.ExaminationRoomDetilListService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;

/** 
 * 类名称：ExaminationControllerController
 * 创建人：FH 
 * 创建时间：2015-04-21
 */
@Controller
@RequestMapping(value="/examinationcontroller")
public class ExaminationControllerController extends BaseController {
	
	@Resource(name="examinationcontrollerService")
	private ExaminationControllerService examinationcontrollerService;
	
	@Resource(name="examinationroomdetillistService")
	private ExaminationRoomDetilListService examinationroomdetillistService;
	
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	public ModelAndView save() throws Exception{
		logBefore(logger, "新增ExaminationController");
		
		PageData pd = new PageData();
		pd = this.getPageData();
		//生产主键id
		String uid = this.get32UUID();
		pd.put("EXAMINATIONCONTROLLER_ID",uid );	//主键
		pd.put("STATE", "1");	//考点状态
		examinationcontrollerService.save(pd);
		//根据保存操作活提交操作确定跳转路径
		String path="";
		if(BusinessConstant.FLAG_YES.equals(pd.get("submitFlag"))){
			path="redirect:/examinationcontroller/list.do";
		}else{
			path="redirect:/examinationcontroller/goEdit.do?EXAMINATIONCONTROLLER_ID="+uid+"&submitFlag=2";
		}
		ModelAndView mv = new ModelAndView(path);
		return mv;
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
		logBefore(logger, "删除ExaminationController");
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			examinationcontrollerService.delete(pd);
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception{
		logBefore(logger, "修改ExaminationController");
		PageData pd = new PageData();
		pd = this.getPageData();
		examinationcontrollerService.edit(pd);
		String path="";
		if(BusinessConstant.FLAG_YES.equals(pd.get("submitFlag"))){
			path="redirect:/examinationcontroller/list.do";
		}else{
			path="redirect:/examinationcontroller/goEdit.do?EXAMINATIONCONTROLLER_ID="+pd.get("EXAMINATIONCONTROLLER_ID")+"&submitFlag=2";
		}
		ModelAndView mv = new ModelAndView(path);
		return mv;
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表ExaminationController");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	varList = examinationcontrollerService.list(page);	//列出ExaminationController列表
			this.getHC(); //调用权限
			mv.setViewName("examination/examinationcontroller/examinationcontroller_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增ExaminationController页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("examination/examinationcontroller/examinationcontroller_edit");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(Page page){
		logBefore(logger, "去修改ExaminationController页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	    String submitFlag = (String) pd.get("submitFlag");
		try {
			pd = examinationcontrollerService.findById(pd);	//根据ID读取
			page.setPd(pd);
			List<PageData>	varList = examinationroomdetillistService.list(page);	//列出ExaminationRoomDetilList列表
			mv.setViewName("examination/examinationcontroller/examinationcontroller_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
			mv.addObject("varList",varList);
			//判断是否由保存操作进入
			if(BusinessConstant.FLAG_NO.equals(submitFlag)){
		    mv.addObject("message",BusinessConstant.MESSAGE_SAVE_OK);//提示保存成功	
			}
			
		} catch (Exception e) {
			logger.error(e.toString(), e);
			mv.addObject("erroMessage",BusinessConstant.MESSAGE_SYSTEM_NG);//提示发生系统错误
		}						
		return mv;
	}	
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除ExaminationController");
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				examinationcontrollerService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(){
		logBefore(logger, "导出ExaminationController到excel");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("考点编码");	//1
			titles.add("考点名称");	//2
			titles.add("考点名称中文");	//3
			titles.add("考点地址");	//4
			titles.add("考点电话");	//5
			titles.add("邮政编码");	//6
			titles.add("考点开启时间");	//7
			titles.add("考点结束时间");	//8
			titles.add("考点状态");	//9
			dataMap.put("titles", titles);
			List<PageData> varOList = examinationcontrollerService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("EXAMINATIONNUM"));	//1
				vpd.put("var2", varOList.get(i).getString("EXAMINATIONNAME"));	//2
				vpd.put("var3", varOList.get(i).getString("EXAMINATIONNAMECN"));	//3
				vpd.put("var4", varOList.get(i).getString("ADDRESS"));	//4
				vpd.put("var5", varOList.get(i).getString("PHONE"));	//5
				vpd.put("var6", varOList.get(i).getString("ZIPCODE"));	//6
				vpd.put("var7", varOList.get(i).getString("EXBEGINTIME"));	//7
				vpd.put("var8", varOList.get(i).getString("EXENDTIME"));	//8
				vpd.put("var9", varOList.get(i).getString("STATE"));	//9
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 去新增列表信息页面
	 */
	@RequestMapping(value="/goListAdd")
	public ModelAndView goListAdd(String EXAMINATIONCONTROLLER_ID){
		logBefore(logger, "去新增ExaminationRoomDetilList页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("examination/examinationcontroller/examinationroomdetillist_edit");
			mv.addObject("msg", "saveListDetail");
			mv.addObject("pd", pd);
			mv.addObject("EXAMINATIONCONTROLLER_ID",EXAMINATIONCONTROLLER_ID);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/**
	 * 新增列表信息详情
	 */
	@RequestMapping(value="/saveListDetail")
	public ModelAndView saveListDetail() throws Exception{
		
		logBefore(logger, "新增ExaminationRoomDetilList");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("EXAMINATIONROOMDETILLIST_ID", this.get32UUID());	//主键
		pd.put("EXAMINATIONID", pd.get("EXAMINATIONCONTROLLER_ID"));	//考点ID
		pd.put("COURSEID", "");	//科目id
		pd.put("STATE", "1");	//考场状态
		examinationroomdetillistService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 去修改列表信息页面
	 */
	@RequestMapping(value="/goListEdit")
	public ModelAndView goListEdit() throws Exception{
		logBefore(logger, "去修改ExaminationRoomDetilList页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = examinationroomdetillistService.findById(pd);	//根据ID读取
			mv.setViewName("examination/examinationcontroller/examinationroomdetillist_edit");
			mv.addObject("msg", "editListDetail");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/**
	 * 修改列表信息页面
	 */
	@RequestMapping(value="/editListDetail")
	public ModelAndView editListDetail() throws Exception{
		logBefore(logger, "修改ExaminationRoomDetilList");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		examinationroomdetillistService.edit(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}	
	
	/**
	 * 删除列表信息页面
	 */
	@RequestMapping(value="/deleteListDetail")
	public void deleteListDetail(PrintWriter out){
		logBefore(logger, "删除ExaminationRoomDetilList");
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			examinationroomdetillistService.delete(pd);
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
	}
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteListDetailAll")
	@ResponseBody
	public Object deleteListDetailAll() {
		logBefore(logger, "批量删除ExaminationRoomDetilList");
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				examinationroomdetillistService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}	
	/* ===============================权限================================== */
	public void getHC(){
		ModelAndView mv = this.getModelAndView();
		HttpSession session = this.getRequest().getSession();
		Map<String, String> map = (Map<String, String>)session.getAttribute(Const.SESSION_QX);
		mv.addObject(Const.SESSION_QX,map);	//按钮权限
		List<Menu> menuList = (List)session.getAttribute(Const.SESSION_menuList);
		mv.addObject(Const.SESSION_menuList, menuList);//菜单权限
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
